package cn.kgc.springboot.mapper;


import cn.kgc.springboot.entity.Usr;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author 课工场
 * @date 2023/11/10
 * @description
 * 1.自定义条件  构造构造器  查询   更新
 * 2. 动态sql语句   condition
 * 3. 分页查询
 */
@SpringBootTest
public class UserMapperTest2 {

    @Resource
    private UserMapper userMapper;


    @Test
    public void getList() {

        List<Usr> usrs = userMapper.selectList(null);
        for (Usr usr : usrs) {
            System.out.println("usr = " + usr);
        }

    }

    @Test
    public void getListByCondition() {   // 通过条件构造器  查询数据列表

        // 1. 创建查询条件构造器对象
        QueryWrapper<Usr> usrQueryWrapper = new QueryWrapper<>();
        // 2. 设置查询条件   SELECT id,username AS uname,password,age,is_delete FROM user WHERE (username = ?)
        //SELECT id,username AS uname,password,age,is_delete FROM user WHERE (username = ? AND password = ?)
        //SELECT id,username AS uname,password,age,is_delete FROM user WHERE (username = ? OR password = ?)

        usrQueryWrapper.eq("username", "tom")
                .or()
                .eq("password", "12345");


        List<Usr> usrs = userMapper.selectList(usrQueryWrapper);

    }


    @Test
    public void getListByCondition2() {

        QueryWrapper<Usr> usrQueryWrapper = new QueryWrapper<>();
        // 根据年龄查询    gt     WHERE (age > ?)     lt   WHERE (age < ?)    ge  WHERE (age >= ?)

        //  WHERE (age >= ? AND age <= ?)
//        usrQueryWrapper.ge("age",15)
//                .le("age",20);

        //WHERE (age BETWEEN ? AND ?)
        usrQueryWrapper.between("age", 15, 20);


        List<Usr> usrs = userMapper.selectList(usrQueryWrapper);

    }


    @Test
    public void getListByCondition3() {

        // 构建模糊查询的条件
        QueryWrapper<Usr> usrQueryWrapper = new QueryWrapper<>();
        //WHERE (username LIKE %zhang% )
//        usrQueryWrapper.like("username","zhang");

        //   %zhang
//        usrQueryWrapper.likeLeft("username","zhang");

        // zhang%
        usrQueryWrapper.likeRight("username", "zhang");

        List<Usr> usrs = userMapper.selectList(usrQueryWrapper);


    }


    @Test
    public void method1() {   //  动态sql   username  age


        Usr usr = new Usr();
//        usr.setAge(30);
//        usr.setUname("jack");

        QueryWrapper<Usr> usrQueryWrapper = new QueryWrapper<>();
//        usrQueryWrapper.like("username",usr.getUname())
//                .eq("age",usr.getAge());


        //  调用重载的eq 方法  条件是否拼接  需要第一个参数返回true
        usrQueryWrapper.like(ObjectUtils.isNotNull(usr.getUname()), "username", usr.getUname())
                .eq(ObjectUtils.isNotNull(usr.getAge()), "age", usr.getAge());

        List<Usr> usrs = userMapper.selectList(usrQueryWrapper);

    }


    @Test
    public void method2() {   //  更新条件的构建

        Usr usr = new Usr();
        usr.setPassword("666666");
        usr.setUname("tom");

        UpdateWrapper<Usr> usrUpdateWrapper = new UpdateWrapper<>();
        usrUpdateWrapper.eq("username", usr.getUname());
        userMapper.update(usr, usrUpdateWrapper);
    }


    @Test
    public void method3() {


        // DELETE FROM user WHERE (age = ?)
        QueryWrapper<Usr> usrQueryWrapper = new QueryWrapper<>();
        usrQueryWrapper.eq("age", 20);


        int delete = userMapper.delete(usrQueryWrapper);
        System.out.println("delete = " + delete);
    }

    @Test
    public void method4(){
        //  分页查询
        Integer page = 1;
        Integer limit = 2;
        String username = null;

        Page<Usr> usrPage = new Page<>(page, limit);

        QueryWrapper<Usr> usrQueryWrapper = new QueryWrapper<>();

        usrQueryWrapper.like(ObjectUtils.isNotNull(username),"username",username);

        userMapper.selectPage(usrPage,usrQueryWrapper);


        // 获取分页数据

        long total = usrPage.getTotal();  //  获取总条数
        List<Usr> records = usrPage.getRecords();

        System.out.println("total = " + total);

        System.out.println("records = " + records);


    }

}